/*
 * Settings.java
 *
 * Created on 16 de Dezembro de 2004, 14:36
 */

package ceirinhashls.util;
import ceirinhashls.DCHub;
import ceirinhashls.PublicHubList;
import java.io.*;
import java.text.*;
import javax.xml.parsers.*;
/**
 *
 * @author  botelhodaniel
 */
public class Settings {
    private static String iniXML = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\" ?>"+
                            "\n<DCHublistServer>";
    private static String endXML = "\n</DCHublistServer>";
    private static String iniSettingsXML = "\n\t<Settings>";
    private static String settingsXML = "\n\t\t<Param key=\"{0}\" value=\"{1}\" />";
    private static String endSettingsXML ="\n\t</Settings>";
    
    private static String iniSessionXML = "\n\t<Session>";
    private static String hubsXML = "\n\t\t<Hub netAddress=\"{0}\" lastUpdate=\"{1}\" name=\"{2}\" address=\"{3}\" " +
    		"description=\"{4}\" users=\"{5}\" shared=\"{6}\" country=\"{7}\" status=\"{8}\" minShare=\"{9}\" " +
    		"minSlots=\"{10}\" maxHubs=\"{11}\" maxUsers=\"{12}\" reliability=\"{13}\" rating=\"{14}\" " +
    		"port=\"{15}\" totalConnections=\"{16}\" faildConnections=\"{17}\" />";
    private static String endSessionXML = "\n\t</Session>";

    public static int PORT;
    public static boolean USE_SESSION;
    public static boolean USE_TABLE_COMPARATOR;
    public static String FTP_SERVER_ADDRESS;
    public static String FTP_SERVER_USER;
    public static String FTP_SERVER_PASSWORD;
    public static String LOCAL_FILE_PATH;
    public static String REMOTE_FILE_PATH;
    public static String OPEN_HUBLIST;
    public static boolean LOCAL_FILE;
    public static boolean WEB_SERVER;
    public static boolean SCHEDULED;
    public static boolean PING_ALL;
    public static String SCHEDULED_HOUR;
    public static String SCHEDULED_MIN;
    //public static String CONFIG_FORMAT;
    
    public static void setDefault(){
    	PORT = 2501;
    	USE_SESSION = false;
    	USE_TABLE_COMPARATOR = false;
        FTP_SERVER_ADDRESS = "ftp.homepages.sapo.pt";
        FTP_SERVER_USER = "devilishly.no.sapo.pt";
        FTP_SERVER_PASSWORD = "";
        LOCAL_FILE_PATH = "c:\\PublicHubList.config";
        REMOTE_FILE_PATH = "PublicHubList.config";
        OPEN_HUBLIST = "http://p2p-pt.com/listadinamica/PublicHubList.config";
        LOCAL_FILE = true;
        WEB_SERVER = false;
        SCHEDULED = false;
        PING_ALL = true;
        SCHEDULED_HOUR = "00";
        SCHEDULED_MIN = ":00";
        //CONFIG_FORMAT = XMLUtil.validateXML("<R: %[reliability]%,S:%[status]> ");
    }
    
    public static void loadSettings(String file,PublicHubList hublist) throws Exception{
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            parser.parse(new File(file), new XMLDefaultHandler(hublist));
    }
    
    public static void saveSettings(String file, java.util.Iterator it) throws Exception{
        PrintWriter out = new PrintWriter(new FileOutputStream(file));
        out.print(iniXML);
		saveSettings(out);
		if (USE_SESSION)
			saveSession(out, it);
		out.print(endXML);
		out.flush();
        out.close();
    }
    
    private static void saveSettings(PrintWriter out){
    	out.print(iniSettingsXML);
    	out.print(MessageFormat.format(settingsXML,new Object[]{"USE_SESSION",USE_SESSION+""}));
    	out.print(MessageFormat.format(settingsXML,new Object[]{"USE_TABLE_COMPARATOR",USE_TABLE_COMPARATOR+""}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"FTP_SERVER_ADDRESS",FTP_SERVER_ADDRESS}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"FTP_SERVER_USER",FTP_SERVER_USER}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"FTP_SERVER_PASSWORD",FTP_SERVER_PASSWORD}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"LOCAL_FILE_PATH",LOCAL_FILE_PATH}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"REMOTE_FILE_PATH",REMOTE_FILE_PATH}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"OPEN_HUBLIST",OPEN_HUBLIST}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"LOCAL_FILE",LOCAL_FILE+""}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"WEB_SERVER",WEB_SERVER+""}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"SCHEDULED",SCHEDULED+""}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"PING_ALL",PING_ALL+""}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"SCHEDULED_HOUR",SCHEDULED_HOUR+""}));
        out.print(MessageFormat.format(settingsXML,new Object[]{"SCHEDULED_MIN",SCHEDULED_MIN+""}));
        //out.print(MessageFormat.format(settingsXML,new Object[]{"CONFIG_FORMAT",(CONFIG_FORMAT==null)?XMLUtil.validateXML("<R: %[reliability]%,S:%[status]> "):CONFIG_FORMAT}));
    	out.print(endSettingsXML);
    }
    private static void saveSession(PrintWriter out, java.util.Iterator it) {
		out.print(iniSessionXML);
		if (it != null)
			while (it.hasNext()) {
				out.print(MessageFormat.format(hubsXML,((DCHub) it.next()).getSession()));
			}
		out.print(endSessionXML);
	}
    
}
